/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.huangp.dao;

import net.huangp.domain.ErrorLog;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;

/**
 *
 * @author Patrick Huang
 */
@Repository
public class ErrorLogDAOHbnImpl implements ErrorLogDAO{
    //--------------------------------------------------------------------------
    // Static Properties
    //--------------------------------------------------------------------------
    private static final Logger logger = LoggerFactory.getLogger(ErrorLogDAOHbnImpl.class);

    public static final String QUERY_DELETE_ERROR_LOG = "delete ErrorLog log where log.id = ?";
    //--------------------------------------------------------------------------
    // Properties
    //--------------------------------------------------------------------------
    private HibernateTemplate hibernateTemplate;
    private SessionFactory factory;
    // -------------------------------------------------------------------------
    // Implementation Methods
    // -------------------------------------------------------------------------
    @Autowired
    public ErrorLogDAOHbnImpl(SessionFactory factory){
        this.factory = factory;
        hibernateTemplate = new HibernateTemplate(this.factory);
    }

    /**
     * insert into error log
     * @param errorLog
     */
    public void insertErrorLog(ErrorLog errorLog) {
        //we want to keep this part silent
        try{
            this.hibernateTemplate.save(errorLog);
        }
        catch(Exception e){
            logger.error("swallowing ErrorLog insertion exception",e);
        }
    }

    /**
     * get error log by id
     * @param id
     * @return
     */
    public ErrorLog getErrorLog(Long id) {
        return this.hibernateTemplate.get(ErrorLog.class, id);
    }

    /**
     * delete an error log by id
     * @param id
     */
    public void removeErrorLog(Long id) {
        this.hibernateTemplate.bulkUpdate(QUERY_DELETE_ERROR_LOG, id);
    }
    // -------------------------------------------------------------------------
    // Mutator Methods
    // -------------------------------------------------------------------------
}
